package com.example.jdimust.ledger;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.jdimust.util.ACache;
import com.example.jdimust.account.AccountActivity;
import com.example.jdimust.util.MessageCheck;
import com.example.jdimust.register.NetworkRequest;
import com.example.jdimust.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: MaZongyang
 * Date: 2021/6/2/0002 12:46
 * Description:
 */
public class LedgaerActivity extends AppCompatActivity {

    private List<LedMes> ledmestList = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ledger);
        ACache acache = ACache.get(this);
        ListView listview = (ListView) findViewById(R.id.lv_ledlist);

        String regetjson =  NetworkRequest.getaccount("/GetMessage/ledger","","");
        Gson gson = new Gson();
        LedgerBean ledgerbean = gson.fromJson(regetjson,LedgerBean.class);

        String ledneme,ledhash,ledid,ledstate;
        for (LedgerBean.ResultDTO resultDTO : ledgerbean.getResult()) {
            ledhash = resultDTO.getLedgerHash();
            ledneme = resultDTO.getLedgerName();
            ledid = String.valueOf(resultDTO.getId());
            ledstate = resultDTO.getState();
//            allres = resultDTO.toString();
//            String mesche = "{\n" +
//                    "\"ledgerData\":"+ allres+"\n"+
//                    "}";
            String restate = null;

            JSONObject alledmesjson = null;
            try {
                alledmesjson = new JSONObject(regetjson);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String allres = alledmesjson.optString("result", null);
            String mesche = "{\n" +
                    "\"ledgerData\":"+ allres+"\n"+
                    "}";

            try {
                restate = MessageCheck.mescheck("/ledger", mesche);
            } catch (IOException e) {
                e.printStackTrace();
            }
            LedMes ledmes = new LedMes(ledneme, ledhash, ledid, restate);
            ledmestList.add(ledmes);
            TextView tv_ledsta = (TextView) findViewById(R.id.ledmes_lv_ledsta);
            if (restate == "pass"){
                tv_ledsta.setTextColor(Color.GREEN);
            }
        }
        LedMesAdapter adapter = new LedMesAdapter(this, R.layout.allled_item, ledmestList);
        ListView listView = (ListView) findViewById(R.id.lv_ledlist);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView viewByLedId = (TextView) view.findViewById(R.id.ledmes_lv_ledid);
                TextView viewByLedHash = (TextView) view.findViewById(R.id.ledmes_lv_ledhash);
                TextView viewByLedName = (TextView) view.findViewById(R.id.ledmes_lv_ledname);
                System.out.println(viewByLedId.getText());
                String ledgerid = (String) viewByLedId.getText();
                String ledgerName = (String) viewByLedName.getText();
                String ledgerHash = (String) viewByLedHash.getText();
//                Intent intent = new Intent(getContext(), AccountActivity.class);
//                intent.putExtra("ledgerName", viewByLedName.getText());//参数：name、value
//                intent.putExtra("ledgerHash", viewByLedHash.getText());//参数：name、value
//                intent.putExtra("ledgerid", viewByLedId.getText());//参数：name、value

                acache.put("acaledgerid",ledgerid);
                acache.put("acaledgerHash",ledgerHash);
                acache.put("acaledgerName",ledgerName);
                startActivity(new Intent(LedgaerActivity.this, AccountActivity.class));
                Toast.makeText(LedgaerActivity.this, "正在打开：" + viewByLedName.getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}